package com.hooper.iot.face.components;

import io.netty.channel.Channel;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 工具类把客户端id作为key放入map中 把通道放入对应value，相当于做一个绑定
 *
 * @author WANGCHENG
 */

@Component(value = "channelRepository")
public class ChannelRepository {
    private final static Map<String, Channel> CHANNEL_MAP = new ConcurrentHashMap<>();

    public void put(String key, Channel value) {
        CHANNEL_MAP.put(key, value);
    }

    public Channel get(String key) {
        synchronized (this) {
            return CHANNEL_MAP.get(key);
        }
    }

    public void remove(String key) {
        synchronized (this) {
            CHANNEL_MAP.remove(key);
        }
    }

    public int size() {
        return CHANNEL_MAP.size();
    }

    public Map getMap() {
        return CHANNEL_MAP;
    }

}
